{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "生成器函数语法\n",
    "\n",
    "`yield` 这个关键字在 Python 中被称之为“生成器函数”的关键字。使用 `yield` 可以使一个普通的函数变成一个生成器（generator），这样的函数在每次被调用时不会立即执行完毕并返回一个固定的值，而是每次调用时返回序列的下一个值，直到所有的值都被返回。\n",
    "这种方式使得生成器函数在处理大量数据或复杂计算时非常有用，因为它们可以节省内存并允许按需生成数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 不使用yield的函数\n",
    "# 返回列表的函数示例\n",
    "def  return_list ( limit ): \n",
    "    result = [] \n",
    "    for i in  range ( 1 , limit + 1 ): \n",
    "        result.append(i) \n",
    "    # 在传统的方法里我们会显式的return结果\n",
    "    return result \n",
    "\n",
    "# 生成值列表\n",
    "my_list = return_list( 5 ) \n",
    "print (my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "# 返回生成器的函数示例\n",
    "def  Yield_values ( limit ): \n",
    "    count = 1 \n",
    "    while count <= limit: \n",
    "        # 在生成器函数中我们不会显式的return结果\n",
    "        # 这实际上类似return count\n",
    "        yield count \n",
    "        count += 1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "generator"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按需生成值\n",
    "my_gen = Yield_values( 5 ) \n",
    "# 使用了yield的函数返回一个生成器对象\n",
    "type(my_gen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成器需要和迭代进行结合使用\n",
    "for num in my_gen: \n",
    "    print (num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def named_modules_with_params(module: nn.Module, name='', depth_first=True, include_root=False):\n",
    "    if module._parameters and not depth_first and include_root:\n",
    "        yield name, module\n",
    "    for child_name, child_module in module.named_children():\n",
    "        child_name = '.'.join((name, child_name)) if name else child_name\n",
    "        yield from named_modules_with_params(\n",
    "            module=child_module, name=child_name, depth_first=depth_first, include_root=True)\n",
    "    if module._parameters and depth_first and include_root:\n",
    "        yield name, module\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mathmodal",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
